﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace CuocChienQueNha.Class.File
{
    class AudioSetting
    {
        public bool isMusic { get; set; }
        public bool isSound { get; set; }
        string temp;
        static IsolatedStorageFile storage;
        public AudioSetting()
        {
            getSetting();
        }
        public void getSetting()
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("audio_setting.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("audio_setting.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("true;true");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("audio_setting.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    isMusic = Convert.ToBoolean(result[0]);
                    isSound = Convert.ToBoolean(result[1]);
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
        public void SaveSetting(bool isMusic, bool isSound)
        {
            this.isMusic = isMusic;
            this.isSound = isSound;
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("audio_setting.txt"))
            {
                storage.DeleteFile("audio_setting.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("audio_setting.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                writer.WriteLine(isMusic.ToString() + ";" + isSound.ToString());
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
    }
}
